import json
from rest_framework.generics import GenericAPIView
from rest_framework.views import APIView
from rest_framework.mixins import ListModelMixin,CreateModelMixin,RetrieveModelMixin, UpdateModelMixin, \
    DestroyModelMixin
from rest_framework.response import Response
# Create your views here.
from book_drf.serializers import BooksSerialzier
from books.models import BookInfo


class BooksDRF(ListModelMixin,CreateModelMixin,GenericAPIView):
    """
        获取所有图书和保存图书
    """
    serializer_class = BooksSerialzier # 指定当前视图使用那个序列化器
    queryset = BookInfo.objects.all()  # 指定当前视图使用那个查询集数据对象

    def get(self, requst):
       return self.list(requst)

    def post(self, request):
       return self.create(request)


class BookDRF(GenericAPIView,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin):
    """
        获取单一图书和修改删除
    """
    serializer_class = BooksSerialzier  # 指定当前视图使用那个序列化器
    queryset = BookInfo.objects.all()  # 指定当前视图使用那个查询集数据对象

    def get(self, requst, pk):
       return self.retrieve(requst)

    def put(self, request, pk):

        # 4、返回结果
        return self.update(request)

    def delete(self, request, pk):
        # 1、查询数据

        return self.destroy(request)

#逻辑;RetrieveModelMixin,实行单个数据的查询集操作
    # UpdateModelMixin,,实行修改操作
    # DestroyModelMixin  , 实行删除操作
    # ListModelMixin,  实行多个数据订单查询集操作
    # CreateModelMixin,实行数据的保存操作
#五个类必须继承genericAPIview 在创建单个数据和多个数据的 时候需要指定查询集和序列化对象